/*
 * WallGenerator.java
 *
 * Created on January 28, 2007, 8:43 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.sf.iSnake.aimodule;

import java.util.Vector;
import net.sf.iSnake.core.GameAreaCoordinate;
import net.sf.iSnake.core.PositionValidator;
import net.sf.iSnake.core.RandomNumberGenerator;
import net.sf.iSnake.server.ServerCorePre;

/**
 *
 * @author Jitendra
 */
public class WallGenerator {
    final int UP=0,DOWN=1,RIGHT=2,LEFT=3;
    final int UP_RIGHT=4,UP_LEFT=5,DOWN_RIGHT=6,DOWN_LEFT=7,HORIZONTAL=8,VERTICAL=9;
    final int MAX_X=64,MAX_Y=48;
    int dir, dir1, dir2, len, corner;
    
    int playerCount=5;
    Vector wallCoordinate=new Vector();
    RandomNumberGenerator random[]=new RandomNumberGenerator[7];
    PositionValidator wallatpv=new PositionValidator();
    Vector initSnake=new Vector();
    ServerCorePre scpre;
    public WallGenerator(ServerCorePre scp) {
        this.scpre=scp;
        setDir();
        setLen();
        resolve();
        setPos();
        registerToPositionValidator();
    }
    
    public void setDir(){
        random[0]=new RandomNumberGenerator(1,4,10);
        dir=random[0].getRandomNumber();
    }
    public void setLen(){
        if(dir<8)
            random[1]=new RandomNumberGenerator(1,3,76);
        if(dir==HORIZONTAL)
            random[1]=new RandomNumberGenerator(1,3,46);
        if(dir==VERTICAL)
            random[1]=new RandomNumberGenerator(1,3,30);
        len=random[1].getRandomNumber();
        if(dir==HORIZONTAL)
            corner=0;
        else
            corner=len-1;
        if(dir<8){
            random[2]=new RandomNumberGenerator(2,1,len-1);
            while(true){
                corner=random[2].getRandomNumber();
                if(corner<(MAX_Y-2)&&((len-corner)<MAX_X-2))
                    break;
            }
        }
    }
    public void resolve(){
        switch(dir){
           case UP_RIGHT:
                dir1=UP;
                dir2=RIGHT;
                break;
            case UP_LEFT:
                dir1=UP;
                dir2=LEFT;
                break;
            case DOWN_RIGHT:
                dir1=DOWN;
                dir2=RIGHT;
                break;
            case DOWN_LEFT:
                dir1=DOWN;
                dir2=LEFT;
                break;
            case HORIZONTAL:
                dir1=-1;
                dir2=RIGHT;
                break;
            case VERTICAL:
                dir1=UP;
                dir2=-1;
        }
    }
    public void setPos(){
        boolean success;
        random[3]=new RandomNumberGenerator(4,1,MAX_X-1);
        random[4]=new RandomNumberGenerator(4,1,MAX_Y-1);
        int x,y;
        while(true){
            success=true;
            x=random[3].getRandomNumber();
            y=random[4].getRandomNumber();
            switch(dir1){
                case UP:
                     if(corner>(y-1))
                         success=false;
                    break;
                case DOWN:
                     if(MAX_Y-2<(y+corner))
                         success=false;
                     break;
            }
            switch(dir2){
                case RIGHT:
                    if((x+(len-1-corner))>(MAX_X-2))
                        success=false;
                    break;
                case LEFT:
                     if((x-(len-1-corner))<1)
                         success=false;
                    break;
            }
            if(success)
                break;
        }
        wallCoordinate.add(new GameAreaCoordinate(x,y)); 
        for(int i=1;i<=corner;i++){
                switch(dir1){
                    case UP:
                        y=y-1;
                        wallCoordinate.add(new GameAreaCoordinate(x,y)); 
                        break;
                    case DOWN:
                        y=y+1;
                        wallCoordinate.add(new GameAreaCoordinate(x,y)); 
                        break;
                }
         }
         for(int j=corner+1;j<len;j++){
            switch(dir2){
                case RIGHT:
                    x=x+1;
                    wallCoordinate.add(new GameAreaCoordinate(x,y));
                    break;
                case LEFT:
                    x=x-1;
                    wallCoordinate.add(new GameAreaCoordinate(x,y));
                    break;
            }
         }
    }
    public void registerToPositionValidator(){
        wallatpv.addConstrain(wallCoordinate,wallatpv.GAMEAREA_OBJECT_WALL);
    }
    public void initSnakePosition(){
        random[5]=new RandomNumberGenerator(playerCount,0,MAX_X);
        random[6]=new RandomNumberGenerator(playerCount,0,MAX_Y);
        generate();
    }
    public void generate(){
        int x,y;
        initSnake.add(0,new GameAreaCoordinate(-1,-1));
        for(int i=0;i<playerCount;i++){
            if(scpre.isPlayer(i)){
                while(true){
                    x=random[5].getRandomNumber();
                    y=random[6].getRandomNumber();
                    if(wallatpv.isSnakePositionValid(new GameAreaCoordinate(x,y)))
                        break;
                }
                initSnake.add(i+1,new GameAreaCoordinate(x,y));
            }
            else
                initSnake.add(i+1,new GameAreaCoordinate(-1,-1));
        }
    }
    public Vector getWallCoordinate(){
        return wallCoordinate;
    }
}